package org.exeed.aop;

import cn.hutool.extra.servlet.ServletUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.exeed.bean.ResultBean;
import org.exeed.entity.UserEntity;
import org.exeed.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.regex.Pattern;

/**
 * @author Dongz
 * @date 2022-04-05
 * @description
 */
@Slf4j
@Aspect
@Order(1)
@Component
public class LoginAspect {

    @Autowired
    private UserService userService;


    private static final ThreadLocal<Long> timeTreadLocal = new ThreadLocal<>();

    @Pointcut("@annotation(org.exeed.aop.tag.Auth)")
    public void logPointcut() {

    }

    @Around("logPointcut()")
    public Object around(ProceedingJoinPoint joinPoint) {
        try {
            timeTreadLocal.set(System.currentTimeMillis());
            ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = requestAttributes.getRequest();
            //打印请求内容
            String token = request.getHeader("token");
            if (token == null){
                token = request.getParameter("token");
            }

            UserEntity user = userService.check(token);
            if (user!= null){
                return joinPoint.proceed();
            }
        }catch (Throwable throwable){
            throwable.printStackTrace();
        }
        return ResultBean.defeated(115, "未登录");
    }


//    @AfterReturning(returning = "o", pointcut = "logPointcut()")
//    public void methodAfterReturning(Object o) {
//        log.info("===============返回内容===============");
//        if (o != null) {
//            log.info("返回的内容:" + o != null ? o.toString().substring(0,Math.min(o.toString().length(),1024)): null);
//        }
//        log.info("===============返回内容===============");
//        long startTime = timeTreadLocal.get();
//        double callTime = (System.currentTimeMillis() - startTime) / 1000.0;
//        log.info("调用花费时间time = {}s", callTime);
//    }
//
//
//    @AfterThrowing(pointcut = "logPointcut()", throwing = "throwable")
//    public void logThrowing(JoinPoint joinPoint, Throwable throwable) {
//        log.info("***************抛出异常***************");
//        log.info("请求类方法:" + joinPoint.getSignature().getName());
//        log.info("异常内容:" + throwable.getMessage());
//        StackTraceElement[] sarr = throwable.getStackTrace();
//        for (StackTraceElement st : sarr) {
//            log.info(st.toString());
//        }
//        log.info("***************抛出异常***************");
//        long startTime = timeTreadLocal.get();
//        double callTime = (System.currentTimeMillis() - startTime) / 1000.0;
//        log.info("调用花费时间time = {}s", callTime);
//
//    }


}

